package com.zh.boot05webadmin.controller;

import com.zh.boot05webadmin.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.thymeleaf.util.StringUtils;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;

/**
 * @Author: zhanghuan
 * @date: 2022/9/27 21:33
 * @description: create it
 */
@Controller
public class IndexController {

    /**
     * 登录页
     *
     * @return
     */
    @GetMapping(value = {"/", "/login"})
    public String loginPage() {
        return "login";
    }

    @PostMapping("/login")
    public String index(User user, HttpSession session, Model model) {
        if (!StringUtils.isEmpty(user.getUserName()) && "123456".equals(user.getPassword())) {
            // 把登录成功的用户保存起来
            session.setAttribute("loginUser", user);
            return "redirect:/index.html";
        } else {
            model.addAttribute("msg", "账号或密码错误");
            // 回到登录页
            return "login";
        }


    }

    @GetMapping("/index.html")
    public String indexPage(HttpSession session, Model model) {
        // 是否登录 拦截器，过滤器
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser != null) {
            return "index";
        } else {
            model.addAttribute("msg", "请重新登录");
            return "login";
        }

    }
}
